###########################################
## 1.runAnalyses.R
## This script runs our analyses
## Last update: SR 15 Mar 2022
###########################################



    # Create necessary variables for analysis 
      
      analysis_dat <-  dat %>% 
        mutate(arbiter = ifelse(T1_US_UN == 1 | T2_US_no_UN == 1, "USA", "France")) %>%
        mutate(own = ifelse(respondentCountry == arbiter, "Own Country", "Other Country"),
               approval = ifelse(T1_US_UN == 1 | T3_FR_UN == 1, "Multilateral", "Unilateral")) %>%
        mutate(strat =paste0(own, ", ", approval)) %>%
        mutate(favor_binary = ifelse(favor >= 4, 1, 0)) %>%
        mutate(rightful_binary = ifelse(rightful >= 4, 1, 0)) %>%
        mutate(merit_binary = ifelse(merit >= 4, 1, 0)) %>%
        mutate(burden_binary = ifelse(burden >= 4, 1, 0)) %>%
        mutate(law_binary = ifelse(law >= 4, 1, 0)) %>%
        mutate(moral_binary = ifelse(moral >= 4, 1, 0)) %>%
        mutate(respondentCountry = as.factor(respondentCountry)) 


      analysis_dat$strat[analysis_dat$Control == 1] <- "Control"
      analysis_dat$strat <- relevel(as.factor(analysis_dat$strat), ref = "Control")
        
      analysis_dat$Gender <- relevel(as.factor(analysis_dat$Gender), ref = "Male")
      analysis_dat$Education <- relevel(as.factor(analysis_dat$Education), ref = "Less than high school")



    ## Run Models and Create Plot for Outcomes
      
      ## Full sample specifications - no stratification by country
        
        out_nostrat <-  analysis_dat %>%
                  do(broom::tidy(
                    lm(cbind(favor, rightful, merit, burden, law, moral) ~ strat + age + Education + Gender, data =.),
                    conf.int = T)) %>%
                  filter(term != "(Intercept)") %>%
                  mutate(level = str_remove_all(term, "strat"),
                         feature = "strat") %>%
                  rename(lower = conf.low,
                         upper = conf.high) %>%
                  mutate(outcome = case_when(response == "favor" ~ "Support",
                                             response == "rightful" ~ "Legitimacy",
                                             response == "merit" ~ "Merit",
                                             response == "burden" ~ "Burden-Sharing",
                                             response == "law" ~ "Legality",
                                             response == "moral" ~ "Morality")) %>%
                  select(-c(response, term))

      ## Stratified by country - split sample specification
        
        out_cstrat <- analysis_dat %>%
          group_by(respondentCountry) %>%
          do(broom::tidy(
            lm(cbind(favor, rightful, merit, burden, law, moral) ~ strat + age + Education + Gender, data =.),
            conf.int = T)) %>%
          filter(term != "(Intercept)") %>%
          mutate(level = str_remove_all(term, "strat"),
                 feature = "strat") %>%
          rename(lower = conf.low,
                 upper = conf.high) %>%
          mutate(outcome = case_when(response == "favor" ~ "Support",
                                     response == "rightful" ~ "Legitimacy",
                                     response == "merit" ~ "Merit",
                                     response == "burden" ~ "Burden-Sharing",
                                     response == "law" ~ "Legality",
                                     response == "moral" ~ "Morality")) %>%
          select(-c(response, term))



  ## Make Table 1 of OLS Regression Results 

    ols_table_1 <- out_nostrat %>%
      filter(level != "Control") %>%
      mutate(sig = case_when(p.value > 0.05 ~ "",
                             p.value <= 0.05 & p.value > 0.01 ~ "x",
                             p.value <= 0.01 & p.value > 0.001 ~ "xx",
                             p.value <= 0.001  ~ "xxx")) %>% 
      mutate(level = str_remove_all(level, "Education"),
             level = str_remove_all(level, "Gender"),
             level = str_replace_all(level, "age", "in Decades")) %>%
      mutate(print_est = paste(round(estimate, digits = 3), sig, "\n(", round(p.value, digits = 3), ")", sep = "")) %>% 
      pivot_wider(id_cols = c(level), names_from = c(outcome), values_from = c(print_est)) %>%
      mutate(across(.cols = everything(), .fns = ~linebreak(.x, align = "c"))) %>%
      mutate(across(.cols = everything(), .fns = ~str_replace_all(.x, "x", "*"))) %>%
      mutate(across(.cols = everything(), .fns = ~str_replace_all(.x, "xx", "**"))) %>%
      mutate(across(.cols = everything(), .fns = ~str_replace_all(.x, "xx", "***"))) 
    
    
    
    
    ## Create OLS Table 1 -- Main Text
    
      ols_table_1 %>% 
        kable(escape = F,
              col.names = c("", "Support", "Legitimacy", "Merit", "Burden-Sharing", "Legality", "Morality"),
              align = c("l", "c", "c", "c", "c", "c", "c"),
              booktabs = T,
              'latex'
        ) %>%
        group_rows("Strike Attributes", 1, 4) %>%
        group_rows("Age", 5, 5) %>% 
        group_rows("Education", 6, 10) %>% 
        group_rows("Gender", 11, 12) %>% 
        save_kable('output/tables/ols_table_1.tex',float = FALSE)
      
      

      
 
      

